package com.zwb.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zwb.boot.common.R;
import com.zwb.boot.entity.ShoppingCart;
import com.zwb.boot.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 头发又黑又长
 * @Date 2022/7/25 18:19
 * @email zwb15083976291@163.com
 */
@Api("购物车管理")
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("获取购物车列表")
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId)
                .orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    @ApiOperation("添加购物车数据")
    @PostMapping("/add")
    public R<Object> add(@RequestBody ShoppingCart shoppingCart,
                         HttpServletRequest request) {

        if (ObjectUtils.isEmpty(shoppingCart.getDishId()) && ObjectUtils.isEmpty(shoppingCart.getSetmealId())) {
            return R.success("缺少重要参数");
        }
        if (!ObjectUtils.isNotEmpty(shoppingCart.getDishId()) && !ObjectUtils.isNotEmpty(shoppingCart.getSetmealId())) {
            return R.success("参数错误");
        }
        Long userId = (Long) request.getSession().getAttribute("user");
        shoppingCart.setUserId(userId);

        ShoppingCart shoppingCartServiceOne = shoppingCartService.findByUserIdWithDishIdOrSetmealId(shoppingCart);
        if (ObjectUtils.isEmpty(shoppingCartServiceOne)) {
            // 保存到数据库
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne = shoppingCart;
        } else {
            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartServiceOne);
        }
        return R.success(shoppingCartServiceOne);
    }


    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public R<String> clean(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物成功");
    }

    @ApiOperation("移除购物车")
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(shoppingCart.getDishId()) && ObjectUtils.isEmpty(shoppingCart.getSetmealId())) {
            return R.error("缺少重要参数");
        }
        if (!ObjectUtils.isNotEmpty(shoppingCart.getDishId()) && !ObjectUtils.isNotEmpty(shoppingCart.getSetmealId())) {
            return R.error("参数错误");
        }
        Long userId = (Long) request.getSession().getAttribute("user");
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCartOne = shoppingCartService.findByUserIdWithDishIdOrSetmealId(shoppingCart);
        if (ObjectUtils.isEmpty(shoppingCartOne)) {
            return R.error("数据不存在");
        }
        if (shoppingCartOne.getNumber() == 1) {
            shoppingCartService.removeById(shoppingCartOne.getId());
        } else {
            shoppingCartOne.setNumber(shoppingCartOne.getNumber() - 1);
            shoppingCartService.updateById(shoppingCartOne);
        }
        return R.success(shoppingCartOne);
    }


}
